<template>
  <h1>最近一个月商品总量{{ shopCount }}件</h1>
  <h1>上架{{ shopCountUp }}件</h1>
  <h1>下架{{ shopCountDown }}件</h1>
  <div>
    <el-row>
      <el-col :span="8">
        <div id="main" style="width: 600px; height: 400px"></div>
      </el-col>
      <el-col :span="8">
        <div id="main2" style="width: 600px; height: 400px"></div>
      </el-col>
      <el-col :span="8">
        <div id="main3" style="width: 600px; height: 400px"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import * as echarts from "echarts";
import { onMounted, ref } from "vue";
import { getShopData, getShopDayData } from "../api/api";
const shopCount = ref(0);
const shopCountUp = ref(0);
const shopCountDown = ref(0);

const shopDayBar = () => {
  getShopDayData().then((res) => {
    if (res.data.code == 200) {
      let dayTime = res.data.data.dayTime;
      let upCount = res.data.data.upCount;

      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById("main"));
      // 绘制图表
      myChart.setOption({
        title: {
          text: "最近一个月每天上架的数据",
        },
        tooltip: {},
        xAxis: {
          data: dayTime,
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: upCount,
          },
        ],
      });
    }
  });
};
const shopDayLine = () => {
  getShopDayData().then((res) => {
    if (res.data.code == 200) {
      let dayTime = res.data.data.dayTime;
      let upCount = res.data.data.upCount;

      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById("main2"));
      // 绘制图表
      myChart.setOption({
        title: {
          text: "最近一个月每天上架的数据",
        },
        tooltip: {},
        xAxis: {
          data: dayTime,
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "line",
            data: upCount,
          },
        ],
      });
    }
  });
};
const shopDayPie = () => {
  getShopDayData().then((res) => {
    if (res.data.code == 200) {
      let pieData = res.data.data.pieData;

      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById("main3"));
      // 绘制图表
      myChart.setOption({
        title: {
          text: "最近一个月每天上架的数据",
          subtext: "Fake Data",
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            name: "上架",
            type: "pie",
            radius: "50%",
            data: pieData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    }
  });
};

const onShopData = () => {
  getShopData().then((res) => {
    shopCount.value = res.data.data.shopCount;
    shopCountUp.value = res.data.data.shopCountUp;
    shopCountDown.value = res.data.data.shopCountDown;
  });
};
onMounted(() => {
  onShopData();
  shopDayBar();
  shopDayLine();
  shopDayPie();
});
</script>

<style scoped></style>
